package com.lenin.metro.server;

import java.util.List;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.NotFoundException;
import com.googlecode.objectify.Objectify;
import com.lenin.metro.shared.MapCoordinates;
import com.lenin.metro.shared.ProjectionPoint;
import com.lenin.metro.shared.Tile;
import com.lenin.metro.shared.TileGroup;

public class DataStoreTest {

	private DAO dao;
	
	public DataStoreTest() {
		
		dao = new DAO();
		
	}

	public void put() {

		Objectify ofy = dao.ofy();
		
	}

	public void get() {

	

	}
	
	
	public void createTiles(ProjectionPoint startPoint, int tileSize, int hTiles, int vTiles) {
		
		Objectify ofy = dao.ofy();
		
		MapCoordinates groupCoords = MapCoordinates.pointToCoords(startPoint);
		
		TileGroup tileGroup = new TileGroup(groupCoords);
		ofy.put(tileGroup);
		
		long startLat = groupCoords.getLatitude();
		long startLng = groupCoords.getLongitude();
		
		long lat = startLat;
		long lng = startLng;
		
		for(int i = 0; i < (vTiles*2); i++) {
			
			lat = startLat;
			lng = startLng;
			
			if(i % 2 != 0) {
				lat -= tileSize/2;
				lng += tileSize/2;
			}
			
			for(int j = 0; j < hTiles; j++) {
				
				//System.out.println("get tile at ["+lat+","+lng+"]");
				
				Tile tile = new Tile(new Key<TileGroup>(TileGroup.class, tileGroup.getId()), new MapCoordinates(lat, lng));
				ofy.put(tile);
				
				
				lat -= tileSize;
				lng += tileSize;
				
			}
			
			startLat += tileSize/2;
			startLng += tileSize/2;
			
		}
		
		
	}
	
	
	public TileGroup getTileGroup(MapCoordinates groupCoords, int tileSize, int hTiles, int vTiles) {
		
		Objectify ofy = dao.ofy();
		
		Tile[][] tiles = new Tile[vTiles*2][hTiles];
		
		long startLat = groupCoords.getLatitude();
		long startLng = groupCoords.getLongitude();
		
		long lat = startLat;
		long lng = startLng;
		
		for(int i = 0; i < (vTiles*2); i++) {
			
			lat = startLat;
			lng = startLng;
			
			if(i % 2 != 0) {
				lat -= tileSize/2;
				lng += tileSize/2;
			}
			
			for(int j = 0; j < hTiles; j++) {
				
				Tile tile = null; 
				
				try {
					tile = ofy.get(Tile.class, lat+","+lng);
				} catch(NotFoundException nfe) {
					
				}
				
				tiles[i][j] = tile;
				
				lat -= tileSize;
				lng += tileSize;
				
			}
			
			startLat += tileSize/2;
			startLng += tileSize/2;
			
		}
		
		TileGroup tileGroup = new TileGroup(groupCoords);
		tileGroup.setTileData(tiles);
		tileGroup.setTileSize(tileSize);
		tileGroup.setHorizontalLength(hTiles);
		tileGroup.setVerticalLength(vTiles);
		
		return tileGroup;
		
	}


	public void clearDb() {
		
	}
	

}
